package com.dawn.module.hospital.controller.app.inhospital;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson.JSONObject;
import com.dawn.framework.apisign.core.annotation.Sign;
import com.dawn.framework.common.pojo.CommonResult;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.web.core.util.RequestUtils;
import com.dawn.module.hospital.controller.admin.statics.vo.InhospitalRespVO;
import com.dawn.module.hospital.controller.admin.statics.vo.SearchReqVO;
import com.dawn.module.hospital.controller.app.inhospital.vo.InHospitalPreCreateReqVO;
import com.dawn.module.hospital.controller.app.inhospital.vo.InHospitalOrderQueryReqVO;
import com.dawn.module.hospital.controller.app.inhospital.vo.InHospitalQueryReqVO;
import com.dawn.module.hospital.enums.ErrorCodeConstants;
import com.dawn.module.hospital.service.InHospitalService;
import com.dawn.module.hospital.service.StaticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * @description:
 * @author: cnsu-cmh
 * @time: 2024/8/7 13:45
 */
@Tag(name = "住院 APP - bh")
@RestController
@RequestMapping("/bh/inhospital")
@Validated
public class AppInHospitalController {

    @Autowired
    private InHospitalService inHospitalService;

    @Autowired
    private StaticsService staticsService;

    @Sign
    @PostMapping(value = "/record")
    @Operation(summary = "获取住院列表")
    public CommonResult record(@Valid @RequestBody InHospitalQueryReqVO reqVO){
        return inHospitalService.record(reqVO);
    }

    @Sign
    @Operation(summary = "获取住院信息")
    @PostMapping(value = "/info")
    public CommonResult inHospitalInfo(@Valid @RequestBody InHospitalQueryReqVO reqVO){
        return inHospitalService.info(reqVO);
    }

    @Sign
    @Operation(summary = "创建住院预交订单")
    @PostMapping(value = "/create/pre")
    public CommonResult createPre(@Valid @RequestBody InHospitalPreCreateReqVO reqVO){
        return inHospitalService.createOrder(reqVO);
    }

    @PostMapping(value = "/dopay")
    @Operation(summary = "住院预交下单")
    public CommonResult dopay(HttpServletRequest request) {
        JSONObject jsonObject = RequestUtils.getReqParamJSON(request);
        String orderNumber = jsonObject.getString("orderNumber");
        if (StringUtils.isEmpty(orderNumber)) {
            return CommonResult.error(ErrorCodeConstants.BH_ERROR.getCode(), "订单数据不存在或已失效");
        }
        return inHospitalService.buildPaySignData(orderNumber);
    }

    @Sign
    @Operation(summary = "查询订单信息")
    @PostMapping(value = "/query/order")
    public CommonResult queryOrder(@Valid @RequestBody InHospitalOrderQueryReqVO reqVO) {
        return inHospitalService.queryByOrderNumber(reqVO.getOrderNumber());
    }

    @Sign
    @Operation(summary = "本地住院预交记录")
    @PostMapping("/list")
    public CommonResult<PageResult<InhospitalRespVO>> pageOutpatient(@Valid @RequestBody SearchReqVO pageReqVO){

        if (pageReqVO.getSearchTime() != null && pageReqVO.getSearchTime().length > 1) {
            LocalDateTime beginDateTime = pageReqVO.getSearchTime()[0];
            LocalDateTime endDateTime = pageReqVO.getSearchTime()[1];
            pageReqVO.setBeginDate(LocalDateTimeUtil.formatNormal(beginDateTime));
            pageReqVO.setEndDate(LocalDateTimeUtil.formatNormal(endDateTime));
        }

        PageResult<InhospitalRespVO> pageResult = staticsService.getInhospitalPage(pageReqVO);

        return CommonResult.success(pageResult);
    }

    @Sign
    @PostMapping(value = "/pre/record")
    @Operation(summary = "获取住院预交记录")
    public CommonResult preRecord(@Valid @RequestBody InHospitalQueryReqVO reqVO){
        return inHospitalService.preRecord(reqVO);
    }

    @Sign
    @PostMapping(value = "/pay/record")
    @Operation(summary = "获取住院费用记录")
    public CommonResult payRecord(@Valid @RequestBody InHospitalQueryReqVO reqVO){
        return inHospitalService.payRecord(reqVO);
    }

    @Sign
    @PostMapping(value = "/daily/record")
    @Operation(summary = "获取住院日清单")
    public CommonResult dailyRecord(@Valid @RequestBody InHospitalQueryReqVO reqVO){
        return inHospitalService.dailyRecord(reqVO);
    }

    @Sign
    @PostMapping(value = "/total/cost")
    @Operation(summary = "获取住院总费用")
    public CommonResult totalCost(@Valid @RequestBody InHospitalQueryReqVO reqVO){
        return inHospitalService.totalCost(reqVO);
    }
}
